/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-05-16 20:23:39
 * @LastEditTime: 2022-07-06 20:50:44
 * @LastEditors: liutq
 * @Reference:
 */
package auth

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/maxyang107/jinglan/common"
	"github.com/maxyang107/jinglan/model/user"

	"github.com/maxyang107/jinglan/service"
)

/**
 * @description: 方法描述：用户登录
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func Login(c *gin.Context) {
	var usermsg user.User

	if err := c.ShouldBind(&usermsg); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}
	token, err2 := service.UserService.CheckLogin(&usermsg)
	if err2 != nil {

		common.ResponseToJson(http.StatusOK, common.Fail, err2.Error(), c)
		return
	}
	common.ResponseDetailToJson(http.StatusOK, token, "登录成功", c)
}

/**
 * @description: 方法描述：用户注册
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func Register(c *gin.Context) {
	var requestParam common.RegisterStruct
	if err := c.ShouldBind(&requestParam); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	requestParam.Ip = c.ClientIP()

	err := service.UserService.Register(requestParam)
	if err != nil {
		common.ResponseToJson(http.StatusOK, common.Fail, err.Error(), c)
		return
	}
	common.ResponseToJson(http.StatusOK, common.Ok, "注册成功", c)
}

/**
 * @description: 方法描述：发送验证码
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func SendSmsCode(c *gin.Context) {
	requestParam := common.SmsCode{}
	if err := c.BindQuery(&requestParam); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	requestParam.Ip = c.ClientIP()

	err := service.UserService.SendSmsCode(requestParam)

	if err != nil {
		common.ResponseToJson(http.StatusOK, common.Fail, err.Error(), c)
		return
	}

	common.ResponseToJson(http.StatusOK, common.Ok, "验证码发送成功,验证码5分钟内有效", c)

}

func ModifyPassword(c *gin.Context) {
	var param common.ModifyPassword

	if err := c.ShouldBind(&param); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}
	err := service.UserService.ModifyPassword(param)
	if err != nil {
		common.ResponseToJson(http.StatusOK, common.Fail, err.Error(), c)
		return
	}
	common.ResponseToJson(http.StatusOK, common.Ok, "success!", c)
}
